//
//  ICEEMContacts.m
//  EasyMoney
//
//  Created by 邓乐 on 2020/1/9.
//  Copyright © 2020 zh. All rights reserved.
//

#import "ICEEMContacts.h"
#import "EMContactsUtil.h"
#import "WDPContactChooseController.h"
#import "WDPContactDataModel.h"
#import "ICEJavaScriptBridge.h"
#import "EMContactsUtil.h"

@interface ICEEMSelectedContact : NSObject<YYModel>

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *phone;
@property (nonatomic, strong) NSString *address;

@end

@implementation ICEEMSelectedContact



@end

@interface ICEEMContacts ()<CNContactPickerDelegate>

@property (nonatomic) NSDictionary *params;
@property (nonatomic) NSInteger totalCount;
@property (nonatomic, strong) NSString *selectId;
@property (nonatomic, strong) WDPSelectedContactDataModel *selectedContact;

@end

@implementation ICEEMContacts

+ (instancetype)sharedSingleton {
    static ICEEMContacts *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //不能再使用alloc方法
        //因为已经重写了allocWithZone方法，所以这里要调用父类的分配空间的方法
        _sharedSingleton = [[super allocWithZone:NULL] init];
    });
    return _sharedSingleton;
}

// 防止外部调用alloc 或者 new
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [ICEEMContacts sharedSingleton];
}

// 防止外部调用copy
- (id)copyWithZone:(nullable NSZone *)zone {
    return [ICEEMContacts sharedSingleton];
}

// 防止外部调用mutableCopy
- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return [ICEEMContacts sharedSingleton];
}

- (void)getContacts:(NSDictionary*)params
{
    self.params = params;
    CNContactPickerViewController * contactPickVC = [[CNContactPickerViewController alloc] init];
    contactPickVC.modalPresentationStyle = UIModalPresentationFullScreen;
    contactPickVC.delegate = self;
    contactPickVC.displayedPropertyKeys = @[CNContactPhoneNumbersKey];
    [ICE_ROOT_NAVIGATION_CONTROLLER.topViewController presentViewController:contactPickVC animated:YES completion:nil];
}

#pragma mark - CNContactPickerDelegate
// 获取指定电话
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
    NSString *familyName = contactProperty.contact.familyName;
    if (familyName == nil) familyName = @"";
    NSString *givenName = contactProperty.contact.givenName;
    if (givenName == nil) givenName = @"";
    NSString *name = [NSString stringWithFormat:@"%@%@", familyName ,givenName];
    //电话
    NSString * phoneNum = [contactProperty.value stringValue];
    
    NSDictionary *dit = @{
        @"status":@"success",
        @"selected_contact":@{
                @"name":name,
                @"phone":phoneNum
        },
    };
    [ICEJavaScriptBridge sendReslutToJS:dit callInfo:self.params];
}

- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker
{
    NSDictionary *dit = @{
        @"status":@"error_cancel",
    };
    [ICEJavaScriptBridge sendReslutToJS:dit callInfo:self.params];
}

@end
